package com.thesis.client;

import java.util.Calendar;

import kankan.wheel.widget.OnWheelChangedListener;
import kankan.wheel.widget.WheelView;
import kankan.wheel.widget.adapters.NumericWheelAdapter;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.os.AsyncTask;
import android.os.Bundle;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageButton;
import android.widget.ListView;

import com.thesis.conectify.Connectify;
import com.thesis.object.Date;
import com.thesis.object.ListBill;
import com.thesis.utils.Defines;
import com.thesis.utils.Utils;

public class BillActivity extends Activity {
	Calendar cal = Calendar.getInstance();
	
	int yy = cal.get(Calendar.YEAR);
	int mm = cal.get(Calendar.MONTH);
	int dd = cal.get(Calendar.DAY_OF_MONTH);
	
	EditText tv_dayfrom ;
	EditText tv_dayto ;
	ListBill listBill;
	ListView bill;
	Date dateBegin;
	Date dateEnd;
	

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_bill);
		
			bill = (ListView) findViewById(R.id.list_bill);
			tv_dayfrom = (EditText) findViewById(R.id.bi_ed_dayfrom);
			tv_dayto = (EditText) findViewById(R.id.bi_ed_dayto);
		
			ImageButton btn_dayfrom = (ImageButton) findViewById(R.id.bi_ib_dayfrom);
			ImageButton btn_dayto = (ImageButton) findViewById(R.id.bi_ib_dayto);
			Button btn_view = (Button) findViewById(R.id.bi_b_view);
			btn_view.setBackground(getResources().getDrawable(R.drawable.button_view));

			tv_dayfrom.setText(new StringBuilder()
					// Month is 0 based, just add 1
					.append(dd).append(" ").append("-").append(mm + 1).append("-")
					.append(yy));
			tv_dayto.setText(new StringBuilder()
					// Month is 0 based, just add 1
					.append(dd).append(" ").append("-").append(mm + 1).append("-")
					.append(yy));

			btn_dayfrom.setOnClickListener(new OnClickListener() {

				@Override
				public void onClick(View arg0) {
					// TODO Auto-generated method stub
					TimepickFrom(BillActivity.this);
				}
			});
			
			btn_dayto.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					// TODO Auto-generated method stub
					TimepickTo(BillActivity.this);
				}
			});
			
			btn_view.setOnClickListener(new OnClickListener() {
				
				@Override
				public void onClick(View v) {
					
					
					if (isValid(dateBegin, dateEnd)){
						new ViewBill().execute();
					}else{
						Utils.sharedInstance().showShortToast(BillActivity.this, 
								getResources().getString(R.string.err_date));
					}
					
				}
			});
			
			bill.setOnItemClickListener(new OnItemClickListener() {

				@Override
				public void onItemClick(AdapterView<?> l, View v, int position, long _id) {
					String[] a ={""+listBill.getListBill().get(position).getBillCode(),
							listBill.getListBill().get(position).getEndTime(),
							""+listBill.getListBill().get(position).getTotalCost()};
					Intent intent = new Intent(BillActivity.this,BillDetailStatictisActivity.class);
					intent.putExtra(Defines.BILL_DETAIL,a);
					
					startActivity(intent);
				}
			});
	}

//	@Override
//	public boolean onCreateOptionsMenu(Menu menu) {
//		// Inflate the menu; this adds items to the action bar if it is present.
//		getMenuInflater().inflate(R.menu.bill, menu);
//		return true;
//	}
	
	private void TimepickFrom(final Context cons) {
		LayoutInflater inf = LayoutInflater.from(cons);
		// layout of alert box contains
		View view = inf.inflate(R.layout.wheeldate, null);

		// minute wheel
		final WheelView month = (WheelView) view.findViewById(R.id.month);
		month.setViewAdapter(new NumericWheelAdapter(cons, 1, 12));
		month.setCyclic(false);

		final WheelView year = (WheelView) view.findViewById(R.id.year);
		year.setViewAdapter(new NumericWheelAdapter(cons, yy - 5, yy + 5));
		year.setCyclic(false);

		final WheelView date = (WheelView) view.findViewById(R.id.date);
		date.setViewAdapter(new NumericWheelAdapter(cons, 1, 31));
		date.setCyclic(false);

		OnWheelChangedListener changelistener_month = new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				date.setViewAdapter(new NumericWheelAdapter(
						cons,
						1,
						dayOfmonth(newValue + 1, year.getCurrentItem() - 5 + yy)));
			}
		};
		OnWheelChangedListener changelistener_year = new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				date.setViewAdapter(new NumericWheelAdapter(
						cons,
						1,
						dayOfmonth(month.getCurrentItem() + 1, newValue - 5 + yy)));
			}
		};

		date.setCurrentItem(dd - 1);
		month.setCurrentItem(mm);
		year.setCurrentItem(5);

		month.addChangingListener(changelistener_month);
		year.addChangingListener(changelistener_year);

		new AlertDialog.Builder(cons)
				.setMessage("Chon ngày")
				.setView(view)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						// write code of lines, whatever want to with
						// value of this timepicker
						// hour value= hours.getCurrentItem();
						// minute value = mins.getCurrentItem();

						tv_dayfrom.setText((new StringBuilder()
								// Month is 0 based, just add 1
								.append(date.getCurrentItem() + 1)
								.append("-").append(month.getCurrentItem() + 1)
								.append("-")
								.append(year.getCurrentItem() - 5 + yy)).toString());
						dateBegin  = new Date(date.getCurrentItem() + 1,
								month.getCurrentItem() + 1, year.getCurrentItem() - 5 + yy);
						Log.d("daybegin      ",dateBegin.getStringdate());
						
					}
				})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								return;
							}
						}).create().show();

		// set time

	}

	private void TimepickTo(final Context cons) {
		LayoutInflater inf = LayoutInflater.from(cons);
		// layout of alert box contains
		View view = inf.inflate(R.layout.wheeldate, null);

		// minute wheel
		final WheelView month = (WheelView) view.findViewById(R.id.month);
		month.setViewAdapter(new NumericWheelAdapter(cons, 1, 12));
		month.setCyclic(false);

		final WheelView year = (WheelView) view.findViewById(R.id.year);
		year.setViewAdapter(new NumericWheelAdapter(cons, yy - 5, yy + 5));
		year.setCyclic(false);

		final WheelView date = (WheelView) view.findViewById(R.id.date);
		date.setViewAdapter(new NumericWheelAdapter(cons, 1, dayOfmonth(
				month.getCurrentItem() + 1, year.getCurrentItem() - 5 + yy)));
		date.setCyclic(false);

		OnWheelChangedListener changelistener_month = new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				date.setViewAdapter(new NumericWheelAdapter(
						cons,
						1,
						dayOfmonth(newValue + 1, year.getCurrentItem() - 5 + yy)));
			}
		};
		OnWheelChangedListener changelistener_year = new OnWheelChangedListener() {

			@Override
			public void onChanged(WheelView wheel, int oldValue, int newValue) {
				// TODO Auto-generated method stub
				date.setViewAdapter(new NumericWheelAdapter(
						cons,
						1,
						dayOfmonth(month.getCurrentItem() + 1, newValue - 5 + yy)));
			}
		};

		date.setCurrentItem(dd - 1);
		month.setCurrentItem(mm);
		year.setCurrentItem(5);

		month.addChangingListener(changelistener_month);
		year.addChangingListener(changelistener_year);

		new AlertDialog.Builder(cons)
				.setMessage("Chọn ngày")
				.setView(view)
				.setPositiveButton("OK", new DialogInterface.OnClickListener() {

					@Override
					public void onClick(DialogInterface dialog, int which) {
						// TODO Auto-generated method stub
						// write code of lines, whatever want to with
						// value of this timepicker
						// hour value= hours.getCurrentItem();
						// minute value = mins.getCurrentItem();

						tv_dayto.setText((new StringBuilder()
								// Month is 0 based, just add 1
								.append(date.getCurrentItem() + 1)
								.append("-").append(month.getCurrentItem() + 1)
								.append("-")
								.append(year.getCurrentItem() - 5 + yy)).toString());
						dateEnd  = new Date(date.getCurrentItem() + 1,
								month.getCurrentItem() + 1, year.getCurrentItem() - 5 + yy);
						Log.d("dayend     ",dateEnd.getStringdate());
						
					}
				})
				.setNegativeButton("Cancel",
						new DialogInterface.OnClickListener() {

							@Override
							public void onClick(DialogInterface dialog,
									int which) {
								// TODO Auto-generated method stub
								return;
							}
						}).create().show();

		// set time

	}

	private int dayOfmonth(int month, int year) {
		if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8
				|| month == 10 || month == 12)
			return 31;
		else if (month == 4 || month == 6 || month == 9 || month == 11)
			return 30;
		else if (month == 2 && ((year % 4 == 0) && (year % 100 != 0))
				|| (year % 400 == 0))
			return 29;
		else
			return 28;
	}
	private boolean isValid (Date begin , Date end){
		if ( begin.getYear() <= end.getYear()) return true;
		else {
			if (begin.getMonth() <= end.getMonth()) return true;
			else { 
				if (begin.getDay() <= end.getDay()) return true;
				else return false;
			}
		}
		
	}
	public class ViewBill extends AsyncTask<String, String, String>{

		@Override
		protected String doInBackground(String... params) {
			
			Connectify connect = new Connectify(BillActivity.this);
			Log.d ("TESTTTTTTTTTT","" +dateBegin.getStringdate()+"   "+ dateEnd.getStringdate());
			//listBill = connect.getListBill(dateBegin.getStringdate(), dateEnd.getStringdate());
			listBill = connect.getListBill(dateBegin.getStringdate(), "");
			return null;
		}
		
		@Override
		protected void onPostExecute(String result) {
			super.onPostExecute(result);
			//Log.d("aaaa",""+listBill.getListBill().size());
			bill = (ListView) findViewById(R.id.list_bill);
			bill.setAdapter(new BillAdapter(BillActivity.this,listBill));
		}
	}
}
